package cn.nkpro.elcube.financial;

import camundajar.impl.scala.Int;
import cn.nkpro.elcube.exception.NkInputFailedCaution;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Data
public class NkFinanceMethodOption {
    // 利率
    private Double  ir;
    // 还款次数
    private Integer np;
    // 还款次数/年
    private Integer pf;
    // 现在值
    private Double  pv;
    // 未来值
    private Double  fv;
    // 开始日期
    private Long    dt;
    // 还款日，默认根据dt计算
    private Integer dayOfPay;
    // 开始计息日期，默认为开始日期dt，如果单独指定，则需要计算期初利息
    private Long    dateOfInterest;
    // 起初期末
    private Integer tp = 0;
    // 期间利率 = 年利率 / 付款频率
    private double ip;
    // 付款间隔 单位月
    private int step; //= (int) (12 / params.pf)
    //是否下月支付 0是 1否
    private int isNextMonth;

    // 不等额规则
    private List<NkFinanceCustomRule> customPayAmountRule;

    // 还款金额精度，默认保留2位小数，四舍五入
    private Integer payPrecision = 2;
    @SuppressWarnings("all")
    private Integer payRoundingMode = BigDecimal.ROUND_HALF_UP;

    private List<NkFinanceRate> rates = new ArrayList<>();

    public final boolean validate(){
        if(this.customValidate()){

            if(dt!=null){
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(dt*1000);

                if(calendar.get(Calendar.HOUR)!=0||calendar.get(Calendar.MINUTE)!=0||calendar.get(Calendar.SECOND)!=0||calendar.get(Calendar.MILLISECOND)!=0){
                    throw new NkInputFailedCaution("计息日期不允许包含时间");
                }
            }

            if(dt != null && dayOfPay==null){
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(dt*1000);

                dayOfPay = calendar.get(Calendar.DAY_OF_MONTH);
            }

            if(dateOfInterest==null){
                dateOfInterest = dt;
            }

            if(pf != null && pf!=0){
                this.ip = ir / pf;
                this.step = 12 / pf;
            }else{
                this.ip = 0;
                this.step = 0;
            }
            return true;
        }
        return false;
    }

    protected boolean customValidate(){
        return ir != null && np != null && pf != null && pv != null && fv != null && dt != null && pf!=0;
    }
}